const app = getApp()

Page({
	data: {
		nowDate: '',
		editIndex: -1,
		serviceIndex: 0,
		services: ['老人病人护理','病人卫生护理', '儿童病人护理','老年人护理','母婴护理','卧床病人护理'],
		jobIndex: 0,
		jobs: ['普通护理员','医疗护理员','高级护理员','特级护理员'],
		start_time: '',
		end_time: '',
		region: '',
		address: '',
		description: '',
		del_nocancel: false
	},

	onLoad(options) {
		const now = new Date()
		if(options.index !== 'undefined') {
			this.setData({
				editIndex: options.index,
			})
			this.requests()
		}
		this.setData({
			nowDate: `${now.getFullYear()}-${now.getMonth()}-${now.getDay() + 1}`,
		})
	},
	
	requests:function(){
	  var that = this
	  var key = wx.getStorageSync('key')
	  var uid = wx.getStorageSync('u')
	  var id = that.data.editIndex
	  wx.request({
	    url: app.configData.shop.api_url + '/index.php?ctl=Personal_Resume&met=getJobInfo&typ=json',
	    method: 'GET',
	    data: { k: key, u: uid, id: id },
	    header: {
	      'content-type': 'application/json'
	    },
	    success: function (res) {
	      if (res.data.status == 200) {
				that.setData({
					serviceIndex: res.data.data.service,
					jobIndex: res.data.data.job,
					start_time: res.data.data.start_time,
					end_time: res.data.data.end_time,
					region: res.data.data.region,
					address: res.data.data.address,
					description: res.data.data.description,
				})
	      } else {
				that.show('数据加载失败')
	      }
	
	    }
	  })
	},
	// 输入事件处理
	handleAddress(e) {
	   this.setData({
	     address: e.detail.value // 实时更新到data
	   })
	},
	bindStartChange(e) {
		this.setData({ start_time: e.detail.value })
	},
	bindEndChange(e) {
		this.setData({ end_time: e.detail.value })
	},
  	//省市区
	bindRegionChange: function (e) {
		this.setData({
		  region: e.detail.value
		})
	},
	serviceChange(e) {
			this.setData({ serviceIndex: e.detail.value[0] })
	},
	jobChange(e) {
			this.setData({ jobIndex: e.detail.value[0] })
	},

	onInput(e) {
		let value = e.detail.value;
		// Android 平台换行符处理
		if (wx.getSystemInfoSync().platform === 'android') {
			value = value.replace(/\n/g, ' ');
		}
		this.setData({ description: value });
	},
  
	saveInfo() {
		const that = this
		const key = wx.getStorageSync('key')
		const uid = wx.getStorageSync('u')
		const id = that.data.editIndex
		if(that.data.start_time == ''){
			app.show('请选择开始时间')
		} else if(that.data.end_time == '') {
			app.show('请选择结束时间')
		} else if(that.data.region == '') {
			app.show('请选择工作地点')
		} else if(that.data.address == '') {
			app.show('请输入详细地址')
		} else if(that.data.description == '') {
			app.show('请输入工作内容')
		} else {
			const job_info = {
				service: that.data.serviceIndex,
				job: that.data.jobIndex,
				start_time: that.data.start_time,
				end_time: that.data.end_time,
				region: that.data.region,
				address: that.data.address,
				description: that.data.description,
			}
			const parme = {
				k: key, 
				u: uid,
				id: id,
				job_info: JSON.stringify(job_info),
			};
			wx.request({
				url: app.configData.shop.api_url + '/index.php?ctl=Personal_Resume&met=addResumeJobInfo&typ=json',
				data: parme,
				method: 'POST', 
				header: {
					"Content-Type": "application/x-www-form-urlencoded"
				}, // 设置请求的 header
				success: function (res) {
					if(res.data.status == 200){
						app.show('保存成功')
						wx.redirectTo({
							url: '../personal_resume/personal_resume',
						})
					}else{
						app.show('保存失败')
					}
				},
				fail: function (res) {
				  
				}
			})
		}
	},
	//删除
	delete() {
	  this.setData({
	    del_nocancel: true
	  })
	},
	del_confirm() {
	  this.del()
	},
	del_cancel() {
	  this.setData({
	    del_nocancel: false
	  })
	},
	del() {
		const that = this
		const key = wx.getStorageSync('key')
		const uid = wx.getStorageSync('u')
		const id = that.data.editIndex
		const parme = {
			k: key, 
			u: uid,
			id: id
		};
		wx.request({
			url: app.configData.shop.api_url + '/index.php?ctl=Personal_Resume&met=delResumeJobInfo&typ=json',
			data: parme,
			method: 'POST', 
			header: {
				"Content-Type": "application/x-www-form-urlencoded"
			}, // 设置请求的 header
			success: function (res) {
				if(res.data.status == 200){
					app.show('删除成功')
					wx.redirectTo({
						url: '../personal_resume/personal_resume',
					})
				}else{
					app.show('删除失败')
				}
			},
			fail: function (res) {
			  
			}
		})
	}
  
  
})